package pl.plesken.tools;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import pl.plesken.xml.ConfigParser;
import pl.plesken.xml.ConfigXML;

public class MovieJukeBoxConfig {
        String plugin;
        String media;
        String skin;
        String thetvdb;
        String opensubtitle;
        String opnSbtUser;
        String opnSbtPwd;
        String enableCover;
        String disableMJB;
        String thumbPersp;
        String thumbRefl;
        String thumbHDLogo;
        String thumbTVLogo;
        String thumbRating;
        String postPersp;
        String postRefl;
        String postHDLogo;
        String postTVLogo;    
        boolean s2b_result;
        String b2s_result;
        StringBuffer cfg = new StringBuffer();
        StringBuffer libxml = new StringBuffer();
        StringBuffer confTemp = new StringBuffer();
       
        public String getPlugin() {
                return plugin;
        }


        public void setPlugin(String plugin) {
                this.plugin = plugin;
        }


        public String getMedia() {
                return media;
        }


        public void setMedia(String media) {
                this.media = media;
        }


        public String getSkin() {
                return skin;
        }


        public void setSkin(String skin) {
                this.skin = skin;
        }


        public String getThetvdb() {
                return thetvdb;
        }


        public void setThetvdb(String thetvdb) {
                this.thetvdb = thetvdb;
        }


        public String getOpensubtitle() {
                return opensubtitle;
        }


        public void setOpensubtitle(String opensubtitle) {
                this.opensubtitle = opensubtitle;
        }


        public String getOpnSbtUser() {
                return opnSbtUser;
        }


        public void setOpnSbtUser(String opnSbtUser) {
                this.opnSbtUser = opnSbtUser;
        }


        public String getOpnSbtPwd() {
                return opnSbtPwd;
        }


        public void setOpnSbtPwd(String opnSbtPwd) {
                this.opnSbtPwd = opnSbtPwd;
        }

        public StringBuffer getConfTemp() {
                return confTemp;
        }


        public void setConfTemp(StringBuffer confTemp) {
                this.confTemp = confTemp;
        }
        
        public String getEnableCover() {
			return enableCover;
		}


		public void setEnableCover(String enableCover) {
			this.enableCover = enableCover;
		}


		public String getDisableMJB() {
			return disableMJB;
		}


		public void setDisableMJB(String disableMJB) {
			this.disableMJB = disableMJB;
		}


		public String getThumbPersp() {
			return thumbPersp;
		}


		public void setThumbPersp(String thumbPersp) {
			this.thumbPersp = thumbPersp;
		}


		public String getThumbHDLogo() {
			return thumbHDLogo;
		}


		public void setThumbHDLogo(String thumbHDLogo) {
			this.thumbHDLogo = thumbHDLogo;
		}


		public String getThumbTVLogo() {
			return thumbTVLogo;
		}


		public void setThumbTVLogo(String thumbTVLogo) {
			this.thumbTVLogo = thumbTVLogo;
		}


		public String getThumbRating() {
			return thumbRating;
		}


		public void setThumbRating(String thumbRating) {
			this.thumbRating = thumbRating;
		}


		public String getPostPersp() {
			return postPersp;
		}


		public void setPostPersp(String postPersp) {
			this.postPersp = postPersp;
		}


		public String getPostHDLogo() {
			return postHDLogo;
		}


		public void setPostHDLogo(String postHDLogo) {
			this.postHDLogo = postHDLogo;
		}


		public String getPostTVLogo() {
			return postTVLogo;
		}


		public void setPostTVLogo(String postTVLogo) {
			this.postTVLogo = postTVLogo;
		}

		public String getThumbRefl() {
			return thumbRefl;
		}


		public void setThumbRefl(String thumbRefl) {
			this.thumbRefl = thumbRefl;
		}


		public String getPostRefl() {
			return postRefl;
		}


		public void setPostRefl(String postRefl) {
			this.postRefl = postRefl;
		}


		public void saveOptions() {
                ConfigParser cnf = new ConfigParser();
                MovieJukeBoxConfig s = new MovieJukeBoxConfig(); 
               
                if (!cnf.readConf("plugins").equals("")) {
                        cnf.saveConf("plugins", cnf.readConf("plugins"), "conf/GUI.xml");
                }
               
                if (!plugin.equals("")) { //$NON-NLS-1$
                        cnf.saveConf("plugin", plugin, "conf/GUI.xml"); //$NON-NLS-1$ //$NON-NLS-2$
                } else {
                        cnf.saveConf("plugin", cnf.readConf("plugin"), "conf/GUI.xml"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                }
               
                cnf.saveConf("media", media, "conf/GUI.xml"); //$NON-NLS-1$ //$NON-NLS-2$
               
                if (!skin.equals("")) { //$NON-NLS-1$
                        cnf.saveConf("skin", skin, "conf/GUI.xml"); //$NON-NLS-1$ //$NON-NLS-2$
                } else {
                        cnf.saveConf("skin", cnf.readConf("skin"), "conf/GUI.xml"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                }
               
                cnf.saveConf("thetvdb", thetvdb, "conf/GUI.xml"); //$NON-NLS-1$ //$NON-NLS-2$
                cnf.saveConf("opensubtitle", opensubtitle, "conf/GUI.xml"); //$NON-NLS-1$ //$NON-NLS-2$
                cnf.saveConf("opnSbtUser", opnSbtUser, "conf/GUI.xml"); //$NON-NLS-1$ //$NON-NLS-2$
                cnf.saveConf("opnSbtPwd", opnSbtPwd, "conf/GUI.xml"); //$NON-NLS-1$ //$NON-NLS-2$
                cnf.saveConf("setCover", enableCover, "conf/GUI.xml");
                cnf.saveConf("disableMJB", disableMJB, "conf/GUI.xml");
                cnf.saveConf("thumbPersp", s.string2boolean(thumbPersp), "conf/GUI.xml");
                cnf.saveConf("thumbReflection", s.string2boolean(thumbRefl), "conf/GUI.xml");
                cnf.saveConf("thumbHDlogo", s.string2boolean(thumbHDLogo), "conf/GUI.xml");
                cnf.saveConf("thumbTVlogo", s.string2boolean(thumbTVLogo), "conf/GUI.xml");
                cnf.saveConf("thumbRating", s.string2boolean(thumbRating), "conf/GUI.xml");
                cnf.saveConf("postPersp", s.string2boolean(postPersp), "conf/GUI.xml");
                cnf.saveConf("postReflection", s.string2boolean(postRefl), "conf/GUI.xml");
                cnf.saveConf("postHDlogo", s.string2boolean(postHDLogo), "conf/GUI.xml");
                cnf.saveConf("postTVlogo", s.string2boolean(postTVLogo), "conf/GUI.xml");
                
        }
       
        public void loadTempConf() {
                confTemp.setLength(0);
                try {
                        FileInputStream fin = new FileInputStream("conf/moviejukebox.temp");
                        BufferedReader in = new BufferedReader(new InputStreamReader(fin));
                        String line;
       
                        while ((line = in.readLine()) != null) {
                                if (line.equals("")) {
                                        confTemp.append("\n");
                                } else {
                                        confTemp.append(line + "\n");
                                }
                        }
                        in.close();
                        } catch(Exception e1){
        }
        }
       
        public void saveTempConf() {
                try {
                        FileOutputStream fout = new FileOutputStream("conf/moviejukebox.temp");
                        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fout));
                       
                        out.write(confTemp.toString());
                        out.close();
                       
                        } catch(Exception e1){
        }
        }
       
        public void createProperties() {
                ConfigParser cnf = new ConfigParser();
                MovieJukeBoxConfig load = new MovieJukeBoxConfig();
               
                try {
                        FileOutputStream out = new FileOutputStream("moviejukebox.properties"); //$NON-NLS-1$
                        BufferedWriter f = new BufferedWriter(new OutputStreamWriter(out));
                       
                        load.loadTempConf();
                       
                        cfg.setLength(0);
                        cfg.append(load.getConfTemp().toString() + "\n");
                               
                        if (cnf.readConf("plugin").equals("")) { //$NON-NLS-1$ //$NON-NLS-2$
                                cfg.append("mjb.internet.plugin=com.moviejukebox.plugin.ImdbPlugin\n"); //$NON-NLS-1$
                        } else {
                                cfg.append("mjb.internet.plugin=" + cnf.readConf("plugin") + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                        }
                       
                        if (cnf.readConf("skin").equals("")) { //$NON-NLS-1$ //$NON-NLS-2$
                                cfg.append("mjb.skin.dir=./skins/default\n"); //$NON-NLS-1$
                        } else {
                                cfg.append("mjb.skin.dir=./skins/" + cnf.readConf("skin") + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                        }
                       
                        if (cnf.readConf("media").endsWith("/")) { //$NON-NLS-1$ //$NON-NLS-2$
                                cfg.append("mediainfo.home=" + cnf.readConf("media") + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                        } else {
                                cfg.append("mediainfo.home=" + cnf.readConf("media") + "/\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                        }

                        if (!cnf.readConf("thetvdb").equals("")) { //$NON-NLS-1$ //$NON-NLS-2$
                                cfg.append("thetvdb.language=" + cnf.readConf("thetvdb") + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                        }
                       
                        if (!cnf.readConf("opensubtitle").equals("")) { //$NON-NLS-1$ //$NON-NLS-2$
                                cfg.append("opensubtitles.language=" + cnf.readConf("opensubtitle") + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                        }
               
                        if (!cnf.readConf("opnSbtUser").equals("")) { //$NON-NLS-1$ //$NON-NLS-2$
                                cfg.append("opensubtitles.username=" + cnf.readConf("opnSbtUser") + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                        }
                       
                        if (!cnf.readConf("opnSbtPwd").equals("")) { //$NON-NLS-1$ //$NON-NLS-2$
                                cfg.append("opensubtitles.password=" + cnf.readConf("opnSbtPwd") + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                        }


                        f.write(cfg.toString());
                        f.close();
                       
                } catch(NullPointerException e) {              
                } catch(FileNotFoundException e) {              
                } catch(IOException e) {
                }
        }
       
        public void libraryXML() {
                ConfigXML libXML = new ConfigXML();
       
                try {
                        FileOutputStream lib = new FileOutputStream("libraries.xml"); //$NON-NLS-1$
                        BufferedWriter xml = new BufferedWriter(new OutputStreamWriter(lib));
                       
                        libXML.parseConfXML();
                       
                        libxml.setLength(0);
                        libxml.append("<libraries>\n"); //$NON-NLS-1$
                       
                        for (int i = 0; i < libXML.getRow().size(); i++) {
                                String[] tab = libXML.getRow().get(i).split(";");
                                if (tab[5].toString().equals("enable")) {
                                        libxml.append(" <library>\n"); //$NON-NLS-1$
                                        libxml.append("   <path>" + tab[1].toString() + File.separator + "</path>\n"); //$NON-NLS-1$ //$NON-NLS-2$
                                        libxml.append("   <nmtpath>" + tab[2].toString() + "</nmtpath>\n"); //$NON-NLS-1$ //$NON-NLS-2$
                               
                                        if (!tab[3].toString().equals("empty")) { //$NON-NLS-1$
                                                libxml.append("   <exclude name=\"" + tab[3].toString() + "\"/>\n"); //$NON-NLS-1$ //$NON-NLS-2$
                                        }
                               
                                        libxml.append("   <description>" + tab[0].toString() + "</description>\n"); //$NON-NLS-1$ //$NON-NLS-2$
                                        libxml.append(" </library>\n"); //$NON-NLS-1$  
                                }
                        }

                        libxml.append("</libraries>"); //$NON-NLS-1$
                       
                        xml.write(libxml.toString());
                       
                        xml.close();
                       
                } catch(IOException e) {
                }
        }
       
        public String string2boolean(String word) {
        	
        	if (word.equalsIgnoreCase("tak")) {
        		s2b_result = true;
        	} else if (word.equalsIgnoreCase("yes")) {
        		s2b_result = true;
        	}
        	if (word.equalsIgnoreCase("nie")) {
        		s2b_result = false;
        	} else if (word.equalsIgnoreCase("no")) {
        		s2b_result = false;
        	}

        	return String.valueOf(s2b_result);
        }
        
        
}

